﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EasySL.Controls
{
    public class ExFilterPanel : ChildWindow
    {
        public Action<string, ExFilterValue> OnSubmit { get; set; }
        
        public ExFilterPanel()
        {
            this.Title = new TextBlock()
            {
                FontSize = 12,
                Text = "Filter",
                Margin = new Thickness(3)
            };

            this.Content = grid;
            this.grid.Loaded += new RoutedEventHandler(grid_Loaded);

            this.grid.OnCancel += () => this.Close();

            this.grid.OnSubmit += value =>
            {
                if (OnSubmit != null)
                    OnSubmit(property, value);
                
                this.Close();
            };
        }

        public void SetFilterPanel(string property, ExFilterAction action)
        {
            this.property = property;
            this.action = action;
        }

        void grid_Loaded(object sender, RoutedEventArgs e)
        {
            this.grid.SetFilterPanel(property, action);
        }

        private string property;
        private ExFilterAction action;
        private ExFilterPanelGrid grid = new ExFilterPanelGrid();

    }

    public class ExFilterPanelGrid : ContentControl
    {
        public Action OnCancel { get; set; }
        public Action<ExFilterValue> OnSubmit { get; set; }
        
        public ExFilterPanelGrid()
            : base()
        {
            this.DefaultStyleKey = typeof(ExFilterPanelGrid);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.propertyName = this.GetTemplateChild("propertyName") as ContentPresenter;

            this.submit = this.GetTemplateChild("submit") as Button;
            this.cancel = this.GetTemplateChild("cancel") as Button;

            this.type = this.GetTemplateChild("type") as TextBlock;

            this.value1 = this.GetTemplateChild("value1") as TextBox;
            this.value2 = this.GetTemplateChild("value2") as TextBox;

            this.cancel.Click += (s, e) =>
            {
                if (OnCancel != null)
                    OnCancel();
            };

            this.submit.Click += (s, e) =>
            {
                if (OnSubmit != null)
                {
                    var value = new ExFilterValue();
                    value.Value1 = value1.Text;
                    value.Value2 = value2.Text;

                    OnSubmit(value);
                }
            };
        }

        public void SetFilterPanel(string property, ExFilterAction action)
        {
            this.propertyName.Content = property;
            
            this.value1.Text = string.Empty;
            this.value2.Text = string.Empty;

            this.type.Text = action.ToString();

            if (action != ExFilterAction.Between)
                this.value2.Visibility = Visibility.Collapsed;
            else
                this.value2.Visibility = Visibility.Visible;
        }

        private ContentPresenter propertyName;
        private Button submit;
        private Button cancel;
        private TextBlock type;
        private TextBox value1;
        private TextBox value2;
    }
}
